package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.SysLog;
import com.example.computer_exam_training_sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/log")
@CrossOrigin
public class SysLogController {
    @Autowired
    private SysLogService logService;

    @GetMapping("/search")
    public Map<String, Object> searchLogs(
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String startDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        int offset = (page - 1) * size;
        List<SysLog> list = logService.searchLogsPaged(userName, startDate, offset, size);
        int total = logService.countLogs(userName, startDate);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        return result;
    }

    @PostMapping("/insert")
    public int insertLog(@RequestBody SysLog log) {
        return logService.insertLog(log);
    }
}